<?php
namespace app\api\controller\v1;

use app\api\controller\BaseController;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class Alipay extends BaseController {
    /**
     * 支付宝订单信息接口
     * */
    public function payInfo($money){
        $appId = "2018011301839827";
        $rsaPrivateKey = "MIIEpAIBAAKCAQEA5V7YcTxK6mrBhFkHjvR3dp8mXeRlT7xLqvApoUdUZB7wZMmVN2gYYSUpATrhgiD+oWi2/BwpP8P+TWv8bjjkwbKrKqz0F2rTcIF4gA7ciTBJ5NK6WNKwU+paLkX8XVrzsdWSeX9A5Ic0Ku6waF+YtI6cy2bed2l98J/Y/btq84L3uTJZ0aKIrOHgMgA6h2GcJkTTRV3Bf/Fe9RiigJQ+8FAg4wnL0bWgFKJfx8YwyFOwPaqkB4oTbFqoLXhL1hhR3si07mFb2pyg7eKfvHjlFsvKsL56QwuYqMl0bpw0c9XxCI6IfN/18EgemBVESaMF/JymKRQMXph2Il3FdTmQ7wIDAQABAoIBAQDeZTAH2yXoVMp9wLULbw3F/ln8/88bmL7lZyd2MOsqiLUdhDciGdRJnPY9MxIkUkmRxy5lFAkKwsZ32xRO6usixFIidr/VVMbedyqXP9GlUyTSsV/tb5t4Qepeo40mF2HiM+pp03DgJBf46KYzDT3LE9wDfCKINcje3p5OpsH0iFowk5NcBJ+yM/hlPOnspvl8IuN0a++UQxNe6el49ZNYMBJJxV8CEgLQe9svmzLbjdFSOKD1UVzWo7XChp2bEyJntWXhItOdTIb1ddM/ycJ9fzvMa3p9hJ8kIEu7CbP+3+pRJtwh6TDuPbyZFrjNOuOeYHi56Ckt+nca7b/iPsoBAoGBAPKXSBJARBXwton2iwRnhDK0yD/iIa758iQnXutDcEB703vSTlLpZS0HqovFgrwawdM39VcJ8kPDpP9yEQCkceMQdDCzOPQdQQ1CiOlbPx2ArOhlX/Zp3MY5vm106T8N+NDANsfsYhaOi51sc/z0iuyvWw1VhL9hkFCVQrKLeRahAoGBAPIMfc+cW/vYOf2pDKlvkK49tU0xu34CKsX+0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->build_order_no();
        $aop = new \AopClient;
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $appId;
        $aop->rsaPrivateKey = $rsaPrivateKey;
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest;
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $bizcontent = "{\"body\":\"衣依不舍\","
            . "\"subject\": \"App支付\","
            . "\"out_trade_no\": \"$build_order_no\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$money\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
            . "}";
        $request->setNotifyUrl("http://api.eybs.mgtvshop.com/index/alipay/call_back");
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
        return $response;
//        echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
    }

    /**
     * 获得随机订单号
     * */
    function build_order_no(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    /**
     * 支付宝回调
     * */
    public function call_back(){
        file_put_contents('./runtime/call_back.txt','支付宝授权回调地址');
    }
}